﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace smp.ButterflyCakes.Repository.Implementation
{
    public sealed class CacheSingleton
    {
        private static readonly Lazy<CacheSingleton> _instance = new Lazy<CacheSingleton>(() => new CacheSingleton());
        private static Dictionary<string, dynamic> _tempCache;

        private CacheSingleton()
        {
            _tempCache = new Dictionary<string, dynamic>();
        }

        public static CacheSingleton Instance
        {
            get
            {
                return _instance.Value;
            }
        }

        public void AddTemp(string key, dynamic item)
        {
            if(_tempCache.ContainsKey(key))
            {
                _tempCache.Remove(key);
            }
            _tempCache.Add(key, item);
        }
        
        public dynamic GetTemp(string key, bool destroy)
        {
            if (!_tempCache.ContainsKey(key))
                return null;
            var value = _tempCache[key];
            if(destroy)
            {
                _tempCache.Remove(key);
            }
            return value;
        }
    }
}
